// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify infoTree
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package smartRun {
    import app.SWFInfoTreeEntry;
    import app.configuration.ConfigManger;
    import app.configuration.NodeFileMeta;
    import app.debug.APPLog;

    import com.junkbyte.console.Cc;

    import copyengine.utils.CEStringUtils;
    import copyengine.utils.GeneralUtils;

    import flash.desktop.ClipboardFormats;
    import flash.desktop.NativeDragManager;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MouseEvent;
    import flash.events.NativeDragEvent;
    import flash.filesystem.File;
    import flash.net.URLRequest;

    public final class SmartRunScene {
        private var mGUILoader:Loader;
        private var mSceneContainer:Sprite;
        private var mGUIMc:MovieClip;

        private var mAllSmartNodeFileMetaVector:Vector.<NodeFileMeta>;
        private var mTempExecuteVector:Vector.<NodeFileMeta>;

        private var mCurrentAnalyst:SWFInfoTreeEntry;

        private var mRunBtnMc:MovieClip;

        public function SmartRunScene() {
        }

        //============================================//
        //==Initialize&Dispose  Function
        //============================================//

        public function initialize():void {
            Cc.height = 160;
            mSceneContainer = new Sprite();
            mAllSmartNodeFileMetaVector = new Vector.<NodeFileMeta>();
            mTempExecuteVector = new Vector.<NodeFileMeta>();

            mGUILoader = new Loader();
            mGUILoader.load(new URLRequest("SmartRun.swf"));
            GeneralUtils.addTargetEventListener(mGUILoader.contentLoaderInfo, Event.COMPLETE, onSmartRunGUILoadComplete);
            GeneralUtils.addTargetEventListener(mGUILoader.contentLoaderInfo, IOErrorEvent.IO_ERROR, onSmartRunGUILoadError);
        }

        private function onSmartRunGUILoadError(e:Event):void {
            APPLog.err("Can not load SmartRunGUI");
        }

        private function onSmartRunGUILoadComplete(e:Event):void {
            var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
            var MC:Class = loaderInfo.applicationDomain.getDefinition("SmartRunScene") as Class;
            mGUIMc = new MC();
            mSceneContainer.addChild(mGUIMc);

            mRunBtnMc = mGUIMc["okBtnMc"];

            initializeGUI();
        }

        private function initializeGUI():void {
            mSceneContainer.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onFileDragIn);
            mSceneContainer.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onFileDrop);

            mRunBtnMc.addEventListener(MouseEvent.MOUSE_DOWN, onClickRunBtnMc);
        }

        //============================================//
        //==Public Interface  Function
        //============================================//

        public function getContainer():Sprite { return mSceneContainer; }

        //========================//
        //==Private  Function
        //=========================//

        private function onFileDragIn(e:NativeDragEvent):void {
            NativeDragManager.acceptDragDrop(mSceneContainer);
        }

        private function onFileDrop(e:NativeDragEvent):void {
            var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
            var targetFile:File = files[0];

            var targetFileName:String = CEStringUtils.beforeLast(targetFile.name, ".");

            var allFileNodeMetaVector:Vector.<NodeFileMeta> = ConfigManger.instance.allNodeFileVector;
            for each (var nodeConfigFile:NodeFileMeta in allFileNodeMetaVector) {
                if (nodeConfigFile.fileName == targetFileName) {
                    if (mAllSmartNodeFileMetaVector.indexOf(nodeConfigFile) < 0) {
                        mAllSmartNodeFileMetaVector.push(nodeConfigFile);
                        APPLog.log("SmartRun: add File : " + targetFile.name + " in ");
                    }
                }
            }
        }

        private function onClickRunBtnMc(e:Event):void {
            analyseSWFFile();
        }

        private function analyseSWFFile():void {
            APPLog.log("SwfInfoTree is start , please do not close infoTree...");
            if (mAllSmartNodeFileMetaVector.length > 0) {
                var nodeConfigFile:NodeFileMeta = mAllSmartNodeFileMetaVector.pop();
                mTempExecuteVector.push(nodeConfigFile);

                mCurrentAnalyst = new SWFInfoTreeEntry();
                mCurrentAnalyst.analyzeSWFFile(nodeConfigFile, analyseSWFFile);
                return;
            }
            else {
                var temp:Vector.<NodeFileMeta> = mAllSmartNodeFileMetaVector;
                mAllSmartNodeFileMetaVector = mTempExecuteVector;
                mTempExecuteVector = temp;

                APPLog.log("all finish!");
            }
        }

    }
}
